<!--组件的视图模板，使用HTML语言编写，但是可以使用vue语法，
如Vue指令、插值表达式等-->
<template>
  <div class="goods-card">
    <div class="goods-card__image">
      <img :src="goods.imgUrl" :alt="goods.description || '商品图片'" />
    </div>
    <div class="goods-card__description">
      <p>{{ goods.description || '暂无描述' }}</p>
    </div>
    <div class="goods-card__price">
      <span class="price">￥{{ goods.price || '0.00' }}</span>
      <span class="sales">销量: {{ goods.salesCount || '0' }}</span>
    </div>
  </div>
</template>

<!--组件的数据模型，包括组件的实例创建-->
<script>
export default {
  name: 'GoodsCard',
  props: {
    goods: {
      type: Object,
      required: true,
      default: () => ({
        imgUrl: '',
        description: '',
        price: '',
        salesCount: '',
      }),
      validator: (value) => {
        return (
          'imgUrl' in value &&
          'description' in value &&
          'price' in value &&
          'salesCount' in value
        );
      },
    },
  },
};
</script>

<!--组件的样式，使用css语言编写-->
<style scoped>
.goods-card {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  width: 180px;
  padding: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.goods-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.goods-card__image {
  height: 120px;
  overflow: hidden;
  border-radius: 4px;
  margin-bottom: 8px;
}

.goods-card__image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.goods-card__description p {
  margin: 0;
  font-size: 14px;
  color: #333;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.goods-card__price {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
}

.goods-card__price .price {
  font-size: 16px;
  font-weight: bold;
  color: #ff5722;
}

.goods-card__price .sales {
  font-size: 12px;
  color: #757575;
}
</style>
